`ifndef UVM_ROOT_SVH
`define UVM_ROOT_SVH

class uvm_root extends uvm_component;
	static local uvm_root m_inst;

	extern protected function new();

	extern static function uvm_root get();

	// 被核心服务类使用的内部方法
	static function uvm_root m_uvm_get_root();
		if (m_inst == null) begin
			m_inst = new();
			$display("%s(%0d) create a unique object of singleton class uvm_root using m_uvm_get_root method", `__FILE__, `__LINE__);
		end
		return m_inst;
	endfunction

	extern virtual task run_test(string test_name=""); // 创建测试对象
endclass

const uvm_root uvm_top = uvm_root::get(); // 全局常量对象句柄

function uvm_root uvm_root::get();
	uvm_coreservice_t cs = uvm_coreservice_t::get();
	return cs.get_root();
endfunction


function uvm_root::new();
	super.new("__top__", null);
endfunction

task uvm_root::run_test(string test_name=""); // 创建测试对象
	uvm_object_wrapper test_wrapper;
	uvm_component uvm_test_top;

	if(!$value$plusargs("UVM_TESTNAME=%s", test_name))
		$fatal("+UVM_TESTNAME not found.");
	$display("%s(%0d) running test %s...", `__FILE__, `__LINE__, test_name);

	if (test_name != "") begin
		test_wrapper = uvm_factory::m_type_names[test_name]; // 获取测试类的代理对象句柄
		$cast(uvm_test_top, test_wrapper.create_component(test_name, this)); // 使用代理对象创建测试类对象
	end

	uvm_test_top.build_phase(); // 执行测试对象
	uvm_test_top.connect_phase();
	uvm_test_top.main_phase();
endtask

`endif
